#!/usr/bin/env python3
"""
创建 MySQL 数据库的脚本
"""
import mysql.connector
from mysql.connector import Error


def create_database():
    """创建数据库"""
    connection = None
    try:
        # 连接到 MySQL 服务器（不指定数据库）
        connection = mysql.connector.connect(
            host="localhost", user="root", password="Akimixu198972"
        )

        if connection.is_connected():
            cursor = connection.cursor()

            # 创建数据库
            cursor.execute(
                "CREATE DATABASE IF NOT EXISTS demo_backend CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"
            )
            print("✅ 数据库 'demo_backend' 创建成功")

            # 显示数据库列表
            cursor.execute("SHOW DATABASES")
            databases = cursor.fetchall()
            print("\n📋 当前数据库列表:")
            for db in databases:
                print(f"   - {db[0]}")

    except Error as e:
        print(f"❌ 连接 MySQL 时出错: {e}")

    finally:
        if connection and connection.is_connected():
            cursor.close()
            connection.close()
            print("\n🔐 MySQL 连接已关闭")


if __name__ == "__main__":
    print("🗄️  正在创建 MySQL 数据库...")
    create_database()
